package com.bhs.sansonglogistics.ui.message;

import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseFragment;
import com.bhs.sansonglogistics.bean.VisitorLogBean;
import com.bhs.sansonglogistics.bean.VisitorLogData;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.utils.DateUtils;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 访客记录/联系我的
 */
public class VisitorLogListFragment extends BaseFragment implements NetCallBack {

    private TextView mTvToday;
    private TextView mTvTodayCount;
    private TextView mTvHistory;
    private TextView mTvHistoryCount;
    private RecyclerView mRvList;
    private BaseQuickAdapter<VisitorLogBean, BaseViewHolder> mAdapter;
    private String type;
    private int page = 1;
    private SwipeRefreshLayout mRefreshLayout;


    public static VisitorLogListFragment newInstance(String type) {
        VisitorLogListFragment fragment = new VisitorLogListFragment();
        Bundle args = new Bundle();
        args.putString("type", type);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public int initLayout() {
        return R.layout.fragment_visitor_log_list;
    }

    @Override
    public void initView(View root) {
        type = getArguments().getString("type");
        mTvToday = root.findViewById(R.id.tv_today);
        mTvTodayCount = root.findViewById(R.id.tv_today_count);
        mTvHistory = root.findViewById(R.id.tv_history);
        mTvHistoryCount = root.findViewById(R.id.tv_history_count);
        mRvList = root.findViewById(R.id.rv_list);
        mAdapter = new BaseQuickAdapter<VisitorLogBean, BaseViewHolder>(R.layout.item_visitor_log) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, VisitorLogBean item) {
                Glide.with(getContext()).load(item.getHeaderpic()).into((ImageView) helper.getView(R.id.iv_face));
                if (type.equals("call_mobile")) {
                    Spanned licensePlateNumber = Html.fromHtml("用户<font color=\"#485EF4\">" + item.getMobile() + "</font>" + "通过伞送APP拨打了贵公司电话，请及时处理");
                    helper.setText(R.id.tv_View_details, licensePlateNumber);
                } else {
                    helper.setText(R.id.tv_View_details, String.format("用户%s查看您的公司详情", item.getNickname()));
                }
                helper.setText(R.id.tv_time, DateUtils.getYearMonthDay(item.getCreate_time() * 1000L));
            }
        };
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter.setEmptyView(LayoutInflater.from(getContext()).inflate(R.layout.layout_empty, null));
        mRefreshLayout = root.findViewById(R.id.refreshLayout);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (type.equals("call_mobile")) {
                    MyUtils.callPhone(getContext(), mAdapter.getData().get(position).getMobile());
                }
            }
        });
    }


    @Override
    public void initData() {
        if (type.equals("call_mobile")) {
            mTvToday.setText("今日联系");
            mTvHistory.setText("历史联系");
        }
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadData();

            }
        });

        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                loadData();
            }
        }, mRvList);
        loadData();
    }

    private void loadData() {
        networkRequest(netApi.get_visitor_record(page, 10, type), this);
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        VisitorLogData data = new Gson().fromJson(json, VisitorLogData.class);
        mRefreshLayout.setRefreshing(false);
        if (data.isStatus()) {
            mTvTodayCount.setText(String.valueOf(data.getData().getCount_info().getToday_num()));
            mTvHistoryCount.setText(String.valueOf(data.getData().getCount_info().getHistory_num()));
            if (page == 1) {
                mAdapter.setNewData(data.getData().getList());
            } else {
                mAdapter.loadMoreComplete();
                mAdapter.addData(data.getData().getList());
            }
            if (mAdapter.getData().size() >= data.getData().getTotal_count()) {
                mAdapter.loadMoreEnd();
            }
        }
    }

    @Override
    public void onFailure(int requestCode) {
        mRefreshLayout.setRefreshing(false);
        mAdapter.loadMoreComplete();
    }
}